package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.query.LettersQueryObject;
import cn.wolfcode.trip.base.query.MessageQueryObject;
import cn.wolfcode.trip.base.service.IPrivateLetterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PrivateLetterMessageServiceImpl implements IPrivateLetterMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Override

    //保存私信内容
    public void save(Message message) {
        //设置发送时间

        message.setSendTime(new Date());
        //设置消息类型为私信
        message.setType(3);
            //设置为已读状态,查看自己发送的
            message.setState(0);
        messageMapper.insert(message);
    }

    @Override
    public PageInfo query(LettersQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Message> letters =  messageMapper.queryForLetters(qo);
        return new PageInfo(letters);
    }

    @Override
    public int selectUnReadCount(LettersQueryObject qo) {
        return messageMapper.selectUnReadCountForLetter(qo);
    }

    @Override
    public PageInfo letterForList(LettersQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = messageMapper.selectLetterList(qo);
        for (Map map : list) {
            Object m_state = map.get("m_state");
            if(m_state.equals(1)){
                map.put("count",null);
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void changeMsgState(MessageQueryObject qo) {
        messageMapper.changeMagState(qo);
    }
}
